package ahblue.cn.tanzi.ui.home.video;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.adapter.BRAdapter;
import ahblue.cn.tanzi.adapter.RvHolder;
import ahblue.cn.tanzi.model.home.VideoModel;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import ahblue.cn.tanzi.util.TimeUtils;
import ahblue.cn.tanzi.view.video.MediaPlayer;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author pink-jackstraw
 * @date 2018/11/10
 * @describe
 */
public class HomeVideoFrangment extends Fragment {

    @BindView(R.id.recy)
    RecyclerView recyview;
    @BindView(R.id.refresh)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.rlempty)
    RelativeLayout rlempty;
    private String type;
    private List<VideoModel> dataList = new ArrayList<>();
    private BRAdapter<VideoModel> adapter;
    private int page = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyDataSetChanged();
//                    swipeRefreshLayout.setEnabled(false);
                    break;
                default:
                    break;
            }
        }
    };

    public HomeVideoFrangment() {
        // Required empty public constructor
    }

    public static HomeVideoFrangment newInstance(String type) {
        HomeVideoFrangment fragment = new HomeVideoFrangment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_video, container, false);
        ButterKnife.bind(this, view);
        Bundle args = getArguments();
        if (args != null) {
            type = args.getString("type");
        }
        initView();
//        initLoadMoreListener();
        initData();
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initView() {
        recyview.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new BRAdapter<VideoModel>(getActivity(), R.layout.activity_video_item, dataList) {
            @Override
            protected void convert(RvHolder holder, VideoModel specalModel, int position) {
                holder.setText(R.id.time, TimeUtils.getStandardDate(specalModel.add_time));
                holder.setText(R.id.count, getResources().getString(R.string.commrnt) + specalModel.comment_count + "");
                holder.setText(R.id.name, specalModel.title);
                ImageLoadUtil.load(getActivity(),specalModel.image,(ImageView) holder.getView(R.id.CoverView));
                holder.setText(R.id.lable, specalModel.user_name);
                MediaPlayer medal = holder.getView(R.id.ic_player);
                medal.setVideoPath(specalModel.file, getActivity());
                holder.setOnClickListener(R.id.root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        medal.pausePlay();
                        VideoDetailActivity.into(getActivity(),specalModel.id);
                    }
                });
            }
        };

        recyview.setAdapter(adapter);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setProgressViewEndTarget(true, 100);
        swipeRefreshLayout.setEnabled(false);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dataList.clear();
                        page = 0;
                        initData();
                        mHandler.sendEmptyMessage(1);
                    }
                }).start();
            }
        });
    }

    private void initLoadMoreListener() {

        //对Recyclerview 的一个滑动监听
        recyview.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //判断RecyclerView的状态 是空闲时，同时，是最后一个可见的ITEM时才加载
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    page++;
                    initData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void initData() {
        VideoModel.getVideoList(getActivity(), type, new VideoModel.VideoListResult() {
            @Override
            public void conver(List<VideoModel> model) {
                if (null == model || model.size() == 0) {
                    rlempty.setVisibility(View.VISIBLE);
                    recyview.setVisibility(View.GONE);
                    swipeRefreshLayout.setVisibility(View.GONE);
                } else {
                    dataList.clear();
                    dataList.addAll(model);
                    rlempty.setVisibility(View.GONE);
                    recyview.setVisibility(View.VISIBLE);
                    swipeRefreshLayout.setVisibility(View.VISIBLE);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
